package com.momoauth.api_client.utils;

import com.momoauth.api_client.mapper.UserMapper;
import com.momoauth.common.security.config.SecurityAdapter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SecurityUtils implements SecurityAdapter {

    @Resource
    UserMapper userMapper;

    /**
     * 登录
     * @param username
     * @param password
     */
    @Override
    public Long login(String username, String password) {
        // 提取盐值
        String salt = userMapper.getUserSalt(username);
        // 重新组合密码（原始密码+盐值）
        String pwd = getMD5Pwd(password, salt);
        // 取出用户密码
        String userPwd = userMapper.getUserPassword(username);
        // 判断用户输入的密码是否与数据库中的密码一致
        if (verify(pwd, userPwd)) {
            return userMapper.getUserId(username);
        }
        return null;
    }
}
